package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAccountTraceMapper;
import com.qf.pojo.DtsAccountTrace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author Wy
 * @version V1.0
 * @Project MavenDemo
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/25 17:46
 */
@Service
public class AccoutServiceImpl implements AccoutService {

    @Autowired
    private DtsAccountTraceMapper mapper;
    @Override
    public IPage<DtsAccountTrace> list(String username, String mobile, Integer page, Integer limit, String sort, String desc) {
        IPage<DtsAccountTrace> objectPage = new Page<>(page,limit);


        QueryWrapper<DtsAccountTrace> objectQueryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(username)){
            objectQueryWrapper.eq("username", username);
        }
        if(!StringUtils.isEmpty(mobile)){
            objectQueryWrapper.eq("moblie", mobile);
        }
        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(desc)){
            if(desc.equalsIgnoreCase("asc")){
                objectQueryWrapper.orderByAsc();
            }else {
                objectQueryWrapper.orderByDesc();
            }
        }
        IPage<DtsAccountTrace> dtsAccountTraceIPage = mapper.selectPage(objectPage, objectQueryWrapper);
        return dtsAccountTraceIPage;
    }

    @Override
    public int update(DtsAccountTrace trace) {

           trace.setUpdateTime(new Date());

        int i = mapper.updateById(trace);
        return i;
    }

    @Override
    public DtsAccountTrace findById(Integer id) {
        DtsAccountTrace dtsAccountTrace = mapper.selectById(id);

         return  dtsAccountTrace;
    }
}
